package com.evaluationsystem.system.serivce.impl;

import cn.hutool.core.date.DateUtil;
import com.evaluationsystem.system.dao.AssessInQuiryDao;
import com.evaluationsystem.system.serivce.AssessInQuiryService;
import com.evaluationsystem.system.serivce.WechatPushTaskMessage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AssessInQuiryServiceImpl implements AssessInQuiryService {

    @Autowired
    AssessInQuiryDao dao;

    @Autowired
    WechatPushTaskMessage message;

    @Override
    public Map<String, Object> assessInQuiryTableDatas(Map<String, Object> param) {
        Map<String,Object> map = new HashMap<>();
        PageHelper.startPage(Integer.parseInt(param.get("page").toString()), Integer.parseInt(param.get("limit").toString()));
        //{page=1, limit=10, vselfappmonth=2020-05, vteamappmonth=2020-05, vcustomerappmonth=2020-05, vgroupappmonth=2020-05, checkbox=checkboxFArr}
        param = conditionalFormat(param);
        System.out.println(param);
        List<Map<String, Object>> staffInfos =  dao.assessInQuiryTableDatas(param);
        PageInfo<Map<String, Object>> PageInfos = new PageInfo<>(staffInfos);
        map.put("code","0");//解析接口状态
        map.put("msg","success");//解析提示文本
        map.put("count",PageInfos.getTotal());//解析数据长度
        map.put("data",PageInfos.getList());//解析数据列表
        return map;
    }

    @Override
    public Map<String, Object> notification(Map<String, Object> param) {
        /*vselfappmonth,vteamappmonth,vcustomerappmonth,vgroupappmonth,checkbox=checkboxFArr*/
        Map<String,Object> map = new HashMap<>();
        if ("checkboxFArr".equals(param.get("checkbox").toString())){

            List<Map<String, Object>> staffInfos =  dao.assessInQuiryTableDatas(param);
            try {
                message.pushMeetingCheckinMessage(staffInfos);
            } catch (WxErrorException e) {
                e.printStackTrace();
                map.put("code","5002");//解析接口状态
                map.put("msg","发送失败");//解析提示文本
            }
            map.put("code","200");//解析接口状态
            map.put("msg","发送成功");//解析提示文本
        }else {
            map.put("code","5001");//解析接口状态
            map.put("msg","员工类型不匹配");//解析提示文本
        }
        return map;
    }

    public Map<String,Object> conditionalFormat(Map<String, Object> param){
        /*{page=1, limit=10, vselfappmonth=vselfappmonth, vteamappmonth=vteamappmonth,
         vcustomerappmonth=vcustomerappmonth, vgroupappmonth=vgroupappmonth, checkbox=checkboxTArr}*/
        List<String> strs= Arrays.asList("vselfappmonth,vteamappmonth,vcustomerappmonth,vgroupappmonth".split(",")).stream().collect(Collectors.toList());
        String [] today =  DateUtil.today().split("-");
        strs.forEach(v->param.forEach((key,val)->{ if (val.equals(v))param.put(key,today[0]+"-"+today[1]); }));
        return param;
    }

}
